package org.geektimes.projects.user.web.controller;

import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.service.UserServiceImpl;
import org.geektimes.projects.user.util.validateUtil;
import org.geektimes.web.mvc.controller.PageController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.List;

/**
 * 注册
 *
 * @author huanglk
 */
@Path("/register")
public class RegisterController implements PageController {
    UserServiceImpl service = new UserServiceImpl();

    @Override
    @POST
    @Path("/username")
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Throwable {

        String email = request.getParameter("email");
        String password = request.getParameter("password");
        String username = request.getParameter("username");
        String phoneNumber = request.getParameter("phoneNumber");
        User user = new User();
        user.setEmail(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setPhoneNumber(phoneNumber);

        List<String> validateList = validateUtil.validate(user);
        if (validateList.size() > 0) {
            throw new RuntimeException("请求参数异常:" + validateList.toString());
        }
        boolean userIsRegister = service.register(user);
        if (userIsRegister) {
            request.getRequestDispatcher("index.jsp").forward(request, response);
        } else {
            response.sendRedirect("index.jsp");
        }
        return null;
    }
}
